﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;

namespace HslCommunication.BasicFramework
{
	/// <summary>
	/// 一个简单通用的消息队列
	/// </summary>
	/// <typeparam name="T">类型</typeparam>
	// Token: 0x02000195 RID: 405
	public class SoftMsgQueue<T> : SoftFileSaveBase
	{
		/// <summary>
		/// 实例化一个对象
		/// </summary>
		// Token: 0x060020D4 RID: 8404 RVA: 0x0009FBD0 File Offset: 0x0009DDD0
		public SoftMsgQueue()
		{
			base.LogHeaderText = "SoftMsgQueue<" + typeof(T).ToString() + ">";
		}

		/// <summary>
		/// 临时消息存储的最大条数，必须大于10
		/// </summary>
		// Token: 0x170006B2 RID: 1714
		// (get) Token: 0x060020D5 RID: 8405 RVA: 0x0009FC2C File Offset: 0x0009DE2C
		// (set) Token: 0x060020D6 RID: 8406 RVA: 0x0009FC44 File Offset: 0x0009DE44
		public int MaxCache
		{
			get
			{
				return this.m_Max_Cache;
			}
			set
			{
				bool flag = value > 10;
				if (flag)
				{
					this.m_Max_Cache = value;
				}
			}
		}

		/// <summary>
		/// 获取最新添加进去的数据
		/// </summary>
		// Token: 0x170006B3 RID: 1715
		// (get) Token: 0x060020D7 RID: 8407 RVA: 0x0009FC64 File Offset: 0x0009DE64
		public T CurrentItem
		{
			get
			{
				bool flag = this.all_items.Count > 0;
				T result;
				if (flag)
				{
					result = this.all_items.Peek();
				}
				else
				{
					result = default(T);
				}
				return result;
			}
		}

		/// <summary>
		/// 新增一条数据
		/// </summary>
		// Token: 0x060020D8 RID: 8408 RVA: 0x0009FCA4 File Offset: 0x0009DEA4
		public void AddNewItem(T item)
		{
			object obj = this.lock_queue;
			lock (obj)
			{
				while (this.all_items.Count >= this.m_Max_Cache)
				{
					this.all_items.Dequeue();
				}
				this.all_items.Enqueue(item);
			}
		}

		/// <summary>
		/// 获取存储字符串
		/// </summary>
		/// <returns></returns>
		// Token: 0x060020D9 RID: 8409 RVA: 0x0009FD1C File Offset: 0x0009DF1C
		public override string ToSaveString()
		{
			return JArray.FromObject(this.all_items).ToString();
		}

		/// <summary>
		/// 获取加载字符串
		/// </summary>
		/// <param name="content"></param>
		// Token: 0x060020DA RID: 8410 RVA: 0x0009FD40 File Offset: 0x0009DF40
		public override void LoadByString(string content)
		{
			JArray jarray = JArray.Parse(content);
			this.all_items = (Queue<T>)jarray.ToObject(typeof(Queue<T>));
		}

		/// <summary>
		/// 所有临时存储的数据
		/// </summary>
		// Token: 0x040007C5 RID: 1989
		private Queue<T> all_items = new Queue<T>();

		// Token: 0x040007C6 RID: 1990
		private int m_Max_Cache = 200;

		/// <summary>
		/// 将集合进行锁定
		/// </summary>
		// Token: 0x040007C7 RID: 1991
		private object lock_queue = new object();
	}
}
